package com.sz.admin.system.listener;

import com.sz.admin.system.service.FolderMonitorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 文件监控启动监听器
 * 在应用程序启动完成后自动启动所有启用的文件监控任务
 *
 * @author sz-admin
 * @since 2025-01-13
 */
@Slf4j
@Component
@RequiredArgsConstructor
@Order(1000) // 确保在其他组件初始化后再启动监控
public class FileMonitorStartupListener implements ApplicationListener<ApplicationReadyEvent> {

    private final FolderMonitorService folderMonitorService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("应用程序启动完成，开始启动文件监控服务");
        
        try {
            // 延迟启动，确保所有组件都已完全初始化
            Thread.sleep(3000);
            
            folderMonitorService.startAllMonitors();
            log.info("文件监控服务启动完成");
            
        } catch (Exception e) {
            log.error("启动文件监控服务失败", e);
        }
    }

}